<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class UploadController extends CommonController {
    //列表
    public function index(){
      $upload_object = D('upload');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($upload_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $upload_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','system');
      $this->assign('umark','upload');
      A('list')->setMetaTitle('附件列表')
          ->setBreadCrumb(array('系统','系统功能','附件管理'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('file','详情','picture',60,'false')
          ->addTableColumn('name','名称','text','auto','false')
          ->addTableColumn('path','路径','text','auto','false')
          ->addTableColumn('type','类型','text',100)
          ->addTableColumn('size','大小','size',80)
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',140,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $upload_object = M('upload');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $upload_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'附件状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'附件状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $upload_object = M('upload');
          $map = array(
            'id' => array('in',$ids),
          );
          $idsArr = explode(',',$ids);
          foreach ($idsArr as $key => $value) {
              $up = $upload_object->field('path')->find($value);
              unlink('./Upload/img/'.$up['path']);
          }
          $rs = $upload_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'附件删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'附件删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //图片上传
    public function startUpload(){
        $config = array(
            'maxSize' => 0,
            'rootPath' => './Upload/img/',
            'savePath' => '',
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),
            'subName' => array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info = $upload->upload();
        if($info) {
            foreach($info as $file){
                $item = array(
                  'name' => $file['name'],
                  'path' => $file['savepath'].$file['savename'],
                  'file' => $file['savepath'].$file['savename'],
                  'type' => $file['type'],
                  'size' => $file['size'],
                  'ctime'=> time()
                );
                D('upload')->add($item);
            }
        }
    }
    //图片上传
    public function imgUpload(){
        $config = array(
            'maxSize' => 0,
            'rootPath' => './Upload/img/',
            'savePath' => '',
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),
            'subName' => array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info = $upload->upload();
        $imgid = 0;$ajax = array();
        if($info) {
            foreach($info as $file){
                $item = array(
                  'name' => $file['name'],
                  'path' => $file['savepath'].$file['savename'],
                  'file' => $file['savepath'].$file['savename'],
                  'type' => $file['type'],
                  'size' => $file['size'],
                  'ctime'=> time()
                );
                $imgid = D('upload')->add($item);
                $path = __ROOT__.'/Upload/img/'.$file['savepath'].$file['savename'];;
            }
            $ajax = array(
                'result' => 'ok',
                'id' => $imgid,
                'url' => $path,
            );
            $this->ajaxReturn($ajax);
        }else {
            $ajax = array(
                'result' => 'faild',
                'msg' => '文件上传失败'
            );
        }
    }
    //simditor图片上传
    public function simditorUpload(){
        $config = array(
            'maxSize' => 0,
            'rootPath' => './Upload/img/',
            'savePath' => '',
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),
            'subName' => array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info = $upload->upload();
        $path = '';
        if($info) {
            foreach($info as $file){
                $item = array(
                  'name' => $file['name'],
                  'path' => $file['savepath'].$file['savename'],
                  'file' => $file['savepath'].$file['savename'],
                  'type' => $file['type'],
                  'size' => $file['size'],
                  'ctime'=> time()
                );
                D('upload')->add($item);
                $path = __ROOT__.'/Upload/img/'.$file['savepath'].$file['savename'];
            }
            $ajax['success'] = true;
            $ajax['file_path'] = $path;
        }else {
            $ajax['success'] = false;
            $ajax['file_path'] = '';
        }
        $this->ajaxReturn($ajax);
    }
    //获取图片
    public function getPics(){
        $upload_object = M('upload');
        $map = array(
          'status' => 1,
        );
        $pictures = $upload_object->where($map)->field('path,name,id')->order('ctime desc')->select();
        $this->assign('pictures',$pictures);
        $this->display();
    }
    //获取图片详细信息
    public function getImginfo(){
        if (IS_POST) {
            $staticImgs = array();
            $ids = I('post.ids');
            $upload_object = M('upload');
            $map = array(
              'id' => array('in',$ids),
            );
            $imgs = $upload_object->where($map)->field('path,size,id')->select();
            foreach ($imgs as $key => $value) {
                $staticImgs[$key]['id'] = $value['id'];
                $staticImgs[$key]['size'] = $value['size'];
                $staticImgs[$key]['previewImage'] = __ROOT__.'/Upload/img/'.$value['path'];
                $staticImgs[$key]['isImage'] = true;
                $staticImgs[$key]['status'] = 1;
            }
            $this->ajaxReturn($staticImgs);
        }
    }
}
